package com.ssb.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import com.ssb.service.check.CheckedFrontSysService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 渠道商产品关系查产品列表
 * @author: Hou Dayu
 * @date: Created in 2020/2/10
 */
public class ProductListPluginImpl implements CustomMethodPlugin {

    @Override
    public Object beforeInvoke(Map<String, Object> map) {

        String sId = String.valueOf(map.get("userId"));//渠道商ID
        if(StrUtil.isNull(sId)){
            throw new BusinessException("渠道商ID不能为空");
        }
        String selectType = String.valueOf(map.get("selectType"));
        if(StrUtil.isNull(selectType)) {//不是查询产品公类列表
            String proType = String.valueOf(map.get("proType"));
            String upFlag = StrUtil.doNull(String.valueOf(map.get("upFlag")),"T");
            String sql = getSql(sId, proType, upFlag);
            String rows = StrUtil.doNull(String.valueOf(map.get("rows")), String.valueOf(map.get("perpage")));
            String pageindex = StrUtil.doNull(String.valueOf(map.get("page")), "1");
            int pageNo = Integer.parseInt(pageindex);
            int pageSize = Integer.parseInt(StrUtil.doNull(rows, "10"));
            Page page = (Page) IusPluginUtil.getInstance().select(sql, pageNo, pageSize);
            map.put(CustomMethodPlugin.pluginInnerReturn, page);
        } else {
            String sql = getSqlForProType(sId);
            List list = (List)IusPluginUtil.getInstance().select(sql, 0, 0);
            if(!Validator.isEmpty(list)){
                String dictSql ="select item_value as pro_class_code,item_text as pro_class_name from dict_data_item where cate_ename ='proCategory'";
                List dicts = (List)IusPluginUtil.getInstance().select("plateform",dictSql,0,0);
                List<DataMap> data = new ArrayList<>();
                if(dicts != null){
                    for(int i=0;i<list.size();i++) {
                        DataMap pc = (DataMap) list.get(i);
                        for (int q = 0; q < dicts.size(); q++) {
                            DataMap dm = (DataMap) dicts.get(q);
                            if(pc.get("proType").equals(dm.get("proClassCode"))){
                                data.add(dm);
                            }
                        }
                    }
                }
                map.put(CustomMethodPlugin.pluginInnerReturn,data);
            }
        }
        map.put(CustomMethodPlugin.resultMark, "T");
        return null;
    }

    private String getSql(String sId,String proType,String upFlag){
        StringBuilder sb = new StringBuilder();
        sb.append("select spro.info_id, agent_risk_cname,insur_code,insur_short_name,pro_code,spro.create_time,premium,amount,");
        sb.append("risk_class,risk_name,risk_code,risk_cname,pro_type,up_flag,order_no,pic_url,insure_limit,remark,spr.user_id as s_id");
        sb.append(" from ss_insur_pro spro, ss_org_pro_relation spr where spro.info_id=spr.pro_id ");
        if(StrUtil.isNotNull(sId)) {
            sb.append(" and spr.user_id='"+sId+"'");
        }
        if(StrUtil.isNotNull(proType)) {
            sb.append(" and spro.pro_type='"+proType+"'");
        }
        sb.append(" and spro.up_flag='"+upFlag+"'");
        sb.append(" order by spro.create_time desc");
        return sb.toString();
    }

    private String getSqlForProType(String sId){
        StringBuilder sb = new StringBuilder();
        sb.append("select pro_type ");
        sb.append(" from ss_insur_pro spro, ss_org_pro_relation spr where spro.info_id=spr.pro_id ");
        if(StrUtil.isNotNull(sId)) {
            sb.append(" and spr.user_id='"+sId+"'");
        }
        sb.append(" group by spro.pro_type");
        return sb.toString();
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
}
